package main

import (
	_ "bianwang/web/models"
	"bianwang/web/route"
	"bianwang/web/utils"
	"context"
	httpServer "github.com/asim/go-micro/plugins/server/http/v3"
	"github.com/asim/go-micro/v3"
	"github.com/asim/go-micro/v3/registry"
	"github.com/asim/go-micro/v3/server"
	"github.com/asim/go-micro/v3/util/log"
	"github.com/julienschmidt/httprouter"
	"github.com/topwhere/go-micro/registry/nacos"
	"go.uber.org/zap"
	"net/http"
)

const (
	ServerName = "go.micro.web.Renting" // server name
)

func main() {
	// create new web Server
	srv := httpServer.NewServer(
		server.Name(ServerName),
		server.Address(":8081"),
	)
	//ginRouter := initialize.Routers()

	// register router
	rou := httprouter.New()
	// 映射静态页面
	rou.NotFound = http.FileServer(http.Dir("html"))

	//加载area 的 route
	rou = route.InitAreaRouter(rou)
	//加载oss 的 route
	rou = route.InitOssRouter(rou)
	//加载其他 的 route
	rou = route.InitOtherRouter(rou)

	hd := srv.NewHandler(rou)

	if err := srv.Handle(hd); err != nil {
		log.Fatal(err)
	}

	reg := nacos.NewRegistry(func(options *registry.Options) {
		if options.Context == nil {
			options.Context = context.Background()
		}
		options.Context = context.WithValue(options.Context, nacos.AddressKey{}, utils.GetAddrs())
		options.Context = context.WithValue(options.Context, nacos.ConfigKey{}, utils.GetNacosClientConfig())
	})

	// Create service
	service := micro.NewService(
		micro.Server(srv),
		micro.Registry(reg),
	)

	// Initialise service
	service.Init()

	// Run service
	if err := service.Run(); err != nil {
		zap.S().Debug("客户端运行异常:", err)
	}

}
